<?php
/* $Id$ */

require_once "lib/database/sql/MySQL.class.php";

require_once "FileChecker.class.php";

class FileUpload {

	private $afterUpload_callback				= null;
	
	private $html_file_trigger				= null;

	private $fileChecker_class				= null;

	/* Konstruktor */

	function FileUpload( $trigger ) {
		$this->html_file_trigger = $trigger;

		$this->fileChecker_class = new FileChecker( $this->getCurrentFileObject() );
	}

	function getHTMLFileTrigger() {
		return $this->html_file_trigger;
	}


	function registerProcessCallback( $callback ) {
		$this->afterUpload_callback = $callback;
	}

	/**
         * @param $html_file_trigger im HTML Form uebergebenes Tag
         * @return bool true = Daten erhalten, alles ok; false = Fehler
	 */

	function isUploadOk() {

		//echo "File:<br />";

		//echo $_FILES[$this->getHTMLFileTrigger()]['name']."<br />".
		//	$_FILES[$this->getHTMLFileTrigger()]['tmp_name'];

		if ( $_FILES[$this->getHTMLFileTrigger()] > 0 ) {
			/* Array/Index enthaelt Daten */
			return ($_FILES[$this->getHTMLFileTrigger()]["error"] === UPLOAD_ERR_OK); 
		} else 
			return false;

	}

	function isValidUpload( $acceptedMimeTypes_array ) {
		if ( $this->isUploadOk() ) {
			return $this->getFileChecker_class()->isFileAcceptedMimeFormat( $acceptedMimeTypes_array ); 
		} else
			return false;
	}

	function getCurrentFileObject() {
		return $_FILES[$this->getHTMLFileTrigger()];
	}

	function getCurrentSavedFileTemporaryAbsFilename() {
		$obj = $this->getCurrentFileObject();
		return $obj["tmp_name"];
	}

	function getCurrentPostedFileName() {
		$obj = $this->getCurrentFileObject();
		return $obj["name"];
	}

	function getCurrentFileSize() {
		$obj = $this->getCurrentFileObject();
		return $obj["size"];
	}

	function getCurrentFileMimeType() {
		return $this->getFileChecker_class()->getMimeType();
	}

	function getFileChecker_class() {
		return $this->fileChecker_class;
	}

	function getFileContent() {

		$fileObject = $this->getCurrentFileObject();

		if ( file_exists( $fileObject["tmp_name"] ) ) {
			
			/* Nur das lokal abgespeicherte Archiv/File zum binaren lesen erlauben */
			//$file_d = fopen( "file://".$fileObject["tmp_name"], "rb" ); 
			//$fileContent = fread( $file_d, filesize($file_d) );

			/* Alternativ, Plattform bedingt performanter und ebenfalls binary safe */
			$fileContent = file_get_contents( "file://".$fileObject["tmp_name"] );
			return $fileContent;

		} else {
			return false;
		}


	}

	function saveUploadAsFileCopy( $newfilename ) {

	} 

	function  saveUploadAsSQLCopy( $MySQL_connection_class, $queryTpl, $queryFileDataKeyword ) {

		$queryTpl = str_replace( $queryFileDataKeyword, $fileContent, $queryTpl );

	}

}



?>